package com.xonro.common.histrans.sfeg.biz.cm;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.UUIDGener;
import com.actionsoft.emm.util.DateUtil;
import com.actionsoft.sdk.local.SDK;
import com.xonro.common.histrans.base.ctx.BOCreateData;
import com.xonro.common.histrans.base.ctx.FileTransData;
import com.xonro.common.histrans.base.ctx.HistoryTransContext;
import com.xonro.common.histrans.base.model.HistoryTransLogModel;
import com.xonro.common.histrans.base.consts.SfegConst;
import com.xonro.common.histrans.sfeg.biz.common.SfegProcessTransBiz;
import com.xonro.common.histrans.sfeg.ctx.SfecProcessCreateContext;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class AContractSignBiz extends SfegProcessTransBiz {
    @Override
    protected void transProcess0(SfecProcessCreateContext historyTransContext) {
        String createUser = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getCreateUser();
        String bindId = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getId();
        Timestamp startTime = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getStartTime();
        List<BOCreateData> boCreateDataList = new ArrayList<>();
        RowMap mainData = historyTransContext.getMainData();

        //添加日志
        String mainBOId = UUIDGener.getUUID();
        HistoryTransLogModel logModel = new HistoryTransLogModel();
        historyTransContext.setHistoryTransLogModel(logModel);
        logModel.setBindId(bindId);
        logModel.setBpmBOId(mainBOId);
        logModel.setBpmTable("BO_XR_EPM_A_CONTRACT");//项目基础信息
        logModel.setOuterTable("tb_ContractEconomicItemA");//基础项目信息表
        logModel.setOuterPK(mainData.getString("id"));
        logModel.setOuterExt1(mainData.getString("ctCode"));
        logModel.setOuterExt2(mainData.getString("ctName"));//合同名称
        BOCreateData mainBOCreateData = new BOCreateData();
        boCreateDataList.add(mainBOCreateData);
        BO mainBO = new BO();
        mainBOCreateData.setBo(mainBO);
        mainBOCreateData.setCreateUser(createUser);
        mainBO.setCreateDate(startTime);
        mainBO.setUpdateDate(startTime);
        mainBO.setId(mainBOId);
        mainBO.set("APPLY_NO",  mainData.getString("id"));
        mainBO.set("PROJECT_NO", mainData.getString( "pjCode"));//项目编号
        mainBO.set("PROJECT_NAME", mainData.getString("projectName"));//项目名称
        mainBO.set("LINK_MAIN_CONTRACT_NO", mainData.getString("SSHT"));//主合同编号
        mainBO.set("CONTRACT_STATUS", mainData.getString("ctState"));//合同状态
        mainBO.set("BALANCE_TYPE", DateUtil.parse( mainData.getString("jhkgrq"), "yyyy-MM-dd"));//计划开工日期
        mainBO.set("PLAN_END_DATE", DateUtil.parse( mainData.getString("jhjgrq"), "yyyy-MM-dd"));//计划竣工日期
        mainBO.set("REAL_START_DATE", DateUtil.parse( mainData.getString("sjkgrq"), "yyyy-MM-dd"));//实际开工日期
        mainBO.set("REAL_END_DATE", DateUtil.parse( mainData.getString("sjjgrq"), "yyyy-MM-dd"));//实际竣工日期
        mainBO.set("BALANCE_TYPE", mainData.getString("iType"));//收支类型
        //mainBO.set("ADVANCE_PAY", mainData.getString("imPrest"));//工程预付款
        mainBO.set("MOBILE", mainData.getString("jsdwLxdh"));//建设单位联系电话
        mainBO.set("COMPANY_NAME", mainData.getString("sjdw"));//设计单位
        mainBO.set("LINKMAN", mainData.getString("sjdwLxr"));//设计单位联系人
        historyTransContext.getProcessCreateContext().setBoCreateDatas(boCreateDataList);
        //添加附件
        FileTransData fileTransData = new FileTransData();
        fileTransData.setBoId(mainBOId);
        //OA表订单编号
        fileTransData.setFkCode(mainData.getString("ctCode"));
        fileTransData.setProcessInstId(bindId);
        historyTransContext.getFileTransDatas().add(fileTransData);
        mainBOCreateData.setBoName("BO_XR_EPM_A_CONTRACT");
        fileTransData.setBoName("BO_XR_EPM_A_CONTRACT");
        fileTransData.setFieldName("CONTRACT_ATTACHMENT");
    }

    @Override
    protected String getProcessDefId() {
        return "obj_fec3ce8adbd04bfe8c99fafd23893c03";
    }

    @Override
    protected String getActivityDefId() {
        return "obj_bfe848dd4ccc4716bb02314e9e4e0e14";
    }

    @Override
    protected String getTitle(HistoryTransContext ctx) {
        String ctCode = ctx.getMainData().getString("ctCode");
        String id = ctx.getMainData().getString("id");
        return id+ctCode;
    }

    @Override
    protected String getEnCode(HistoryTransContext ctx) {
        return ctx.getMainData().getString("ctCode");
    }

    @Override
    protected String getOuterTable(SfecProcessCreateContext historyTransContext) {
        return "tb_ContractEconomicItemA";
    }

    @Override
    protected String getPk(SfecProcessCreateContext historyTransContext) {
        return historyTransContext.getMainData().getString("id");
    }

    @Override
    protected List<RowMap> getMainDatas() {
        return SDK.getCCAPI().getRDSAPI(SfegConst.CCID).getMaps("SELECT * FROM tb_ContractInfo info  JOIN tb_ContractEconomicItemA a ON info.ctCode=a.ctCode  WHERE info.belongCorp='1040'");
    }

    @Override
    protected RowMap getMainData() {
        return null;
    }
}
